package com.ly.hotel.service;

import com.ly.hotel.common.MyApplication;
import com.ly.hotel.common.SharedConfBlacklight;
import com.ly.hotel.msgevent.UpdateBlacklightEvent;

import de.greenrobot.event.EventBus;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.WindowManager;
import android.widget.LinearLayout;

/**
* This is the class that is responsible for adding the filter on the
* screen: It works as a service, so that the view persists across all
* activities.
*
* @author Hathibelagal
*/
public class BacklightService extends Service {

	LinearLayout mView;
	SharedConfBlacklight shared;
	
	public static int STATE;
	
	public static final int INACTIVE=0;
	public static final int ACTIVE=0;	
	
	static{
		STATE=INACTIVE;
	}
	
	@Override
	public IBinder onBind(Intent i) {
		return null;
	}

	@Override
	public void onCreate() {
	        super.onCreate();
	        Log.i("BacklightService", "当前亮度：" + MyApplication.softBlacklightValue); 
        	//shared=new SharedConfBlacklight(this);
	        mView = new LinearLayout(this);   
        	mView.setBackgroundColor(MyApplication.softBlacklightValue);
	        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                	WindowManager.LayoutParams.MATCH_PARENT,
        	        WindowManager.LayoutParams.MATCH_PARENT,
	                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                	0 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        	        PixelFormat.TRANSLUCENT);        
	        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        	wm.addView(mView, params);
        	
        	EventBus.getDefault().register(this);
	}
	
	@Override
	public void onDestroy() {			
		super.onDestroy();
		if(mView!=null){
			WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
			wm.removeView(mView);
		}
		EventBus.getDefault().unregister(this);
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {		
		mView.setBackgroundColor(MyApplication.softBlacklightValue);
		return super.onStartCommand(intent, flags, startId);
	}
	
	public void onEventMainThread(UpdateBlacklightEvent e) {
		if(mView != null){
			mView.setBackgroundColor(MyApplication.softBlacklightValue);
		}
	}
}
